package praktikumid.w11.p2;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * Strings - simple operations with objects.
 * A list of objects, where new objects can
 * be added, existing objects can be modified
 * and existing objects can be deleted.
 * <p>
 * We use this simple program to demonstrate
 * undo implementation possibilities.
 * <p>
 * This program is very simple and does not
 * handle any exceptions. This is so to 
 * keep the code simple. Of course, in real
 * world problems (and also in class exercises),
 * all exceptions should be handled properly!
 * <p>
 * Here we have added undo functionality using
 * history list, which basically stores every
 * state into a list. This way it is very easy
 * to undo (or redo).
 * @author Ago
 *
 */
public class ElementUndoHistory {
	/**
	 * Simple element, which technically only has
	 * one String - name. 
	 * @author Ago
	 *
	 */
	static class Element {
		/**
		 * Name of the element.
		 */
		String name;
		
		/**
		 * Constructor, where we set the name.
		 * @param name
		 */
		public Element(String name) {
			this.name = name;
		}
		
		@Override
		public String toString() {
			// let's have a "nice" printout
			return name;
		}
		
		@Override
		protected Object clone() throws CloneNotSupportedException {
			Element e = new Element(this.name);
			return e;
		}
	}
	
	/**
	 * A list of elements.
	 */
	static List<Element> elements = new ArrayList<Element>();
	
	
	static class HistoryState {
		List<Element> elements = new ArrayList<Element>();
	}
	static List<HistoryState> historyStates = new ArrayList<HistoryState>();
	static int historyIndex = -1;
	
	//static List<List<Element>> history = new ArrayList<List<Element>>();
	
	/**
	 * Main.
	 * @param args arguments.
	 * @throws IOException Dealing with input can throw exception.
	 * @throws CloneNotSupportedException 
	 */
	public static void main(String[] args) throws IOException, CloneNotSupportedException {

		BufferedReader br = new BufferedReader(
				new InputStreamReader(System.in));
		while(true) {
			printHelp();
			String line = br.readLine();
			// we expect correct input
			// just split everything by space
			String[] tokens = line.split(" ");
			int op = Integer.valueOf(tokens[0]);
			int elid = -1;
			if (op > 0 && op < 4) {
				newState();
			}
			switch (op) {
			case 1: // add element
				elements.add(new Element(tokens[1]));
				break;
			case 2: // modify element, rename
				elid = Integer.valueOf(tokens[1]);
				Element e = elements.get(elid);
				e.name = tokens[2];
				break;
			case 3: // delete element
				elid = Integer.valueOf(tokens[1]);
				elements.remove(elid);
				break;
			case 4: // printout
				printElements();
				break;
			case 5:
				undo();
				break;
			case 0:
				System.exit(0);
				
			}
			
		}
	}
	
	public static void newState() throws CloneNotSupportedException {
		HistoryState s = new HistoryState();
		historyIndex++;
		while (historyStates.size() > historyIndex) {
			historyStates.remove(historyStates.size() - 1);
		}
		for (Element e : elements) {
			s.elements.add((Element) e.clone());
		}
		historyStates.add(s);
	}
	
	public static void undo() {
		elements = historyStates.get(historyIndex).elements;
		historyIndex--;
	}
	/*
	 * 1 2 3 4 5
	 * U U C R R
	 * U C R R R
	 * U U
	 *   C
	 *   R R R R
	 * U U U
	 *   C 
	 *     R
	 * U U U U U 
	 *   C
	 * 
	 */
	
	/**
	 * Prints out the elements in the list.
	 */
	public static void printElements() {
		for (int i = 0; i < elements.size(); i++) {
			System.out.println(i + ". " + elements.get(i));
		}
	}
	
	/**
	 * Prints out help - what can be done with the program.
	 */
	public static void printHelp() {
		System.out.println("  1 add element [1 name]");
		System.out.println("  2 change element [2 elid name]");
		System.out.println("  3 delete element [3 elid]");
		System.out.println("  4 list elements [4]");
		System.out.println("  5 undo [5]");
		System.out.println("  0 exit [0]");
	}
}

